package com.panda.controller.user;

import com.panda.annotation.SystemLog;
import com.panda.entity.po.User;
import com.panda.entity.vo.ModifyUserVo;
import com.panda.entity.vo.UserPwdVo;
import com.panda.entity.vo.UserVo;
import com.panda.service.ArticleService;
import com.panda.service.UserService;
import com.panda.service.VideoService;
import com.panda.utils.RespondResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;


@Api(tags = "用户接口")
@RestController
@Validated
@RequestMapping("/users")
public class UserController {

    private final UserService userService;
    @Autowired
    private ArticleService articleService;
    @Autowired
    private VideoService videoService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("用户信息")
    @GetMapping("/{id}")
    public RespondResult getUserInfo(@PathVariable Long id) {
        return userService.getUserInfo(id);
    }

    @ApiOperation("更新用户信息")
    @PutMapping
    public RespondResult updateUserInfo(@Validated @RequestBody ModifyUserVo userVo) {
        return userService.updateUserInfo(userVo);
    }

    @ApiOperation("获取某个用户发布的文章")
    @GetMapping("/{id}/articles")
    public RespondResult getArticles(@PathVariable("id") Long id, @NotNull(message = "页码不能为空") Integer pageNum, @NotNull(message = "每页大小不能为空") Integer pageSize,Long categoryId) {
        return articleService.getAllArticles(id, pageNum, pageSize, categoryId, null);
    }

    @ApiOperation("获取某个用户发布的视频")
    @GetMapping("/{id}/videos")
    @SystemLog(businessName = "获取某个用户发布的视频")
    public RespondResult getVideos(@PathVariable("id") Long id, @NotNull(message = "页码不能为空") Integer pageNum, @NotNull(message = "每页大小不能为空") Integer pageSize, Long categoryId) {
        return videoService.getAllVideos(id, pageNum, pageSize, categoryId, null);
    }


    @ApiOperation("更新用户名信息")
    @PutMapping("/username")
    @SystemLog(businessName = "更新用户名信息")
    public RespondResult updateUsername(@Validated @RequestBody UserVo userVo) {
        return userService.updateUsername(userVo);
    }


    @ApiOperation("更新用户密码")
    @PutMapping("/password")
    @SystemLog(businessName = "更新用户密码")
    public RespondResult updatePassword(@Validated @RequestBody UserPwdVo userPwdVo) {
        return userService.updatePassword(userPwdVo);
    }

}
